use "build/output/main_panel.dta", clear

gen p_rounded = round(p, 0.01)

bys coin: egen p_min_01 = pctile(p_min), p(1)
bys coin: egen p_max_99 = pctile(p_max), p(99)

sort coin_id date
tsset coin_id date

gen d_margin_aprFTX_bps = d.margin_aprFTX*100

gen d_p = d.p*1000000


eststo clear
eststo: reg d_p d_margin_aprFTX_bps if (l.p_min<p_min_01 | l.p_max>p_max_99) , r
estadd local coin_fe "No"
estadd local cond_repeg "No"

eststo: reg d_p d_margin_aprFTX_bps i.coin_id if (l.p_min<p_min_01 | l.p_max>p_max_99) , r
estadd local coin_fe "Yes"
estadd local cond_repeg "No"

eststo: reg d_p d_margin_aprFTX_bps i.coin_id iv_btc funding_apr if (l.p_min<p_min_01 | l.p_max>p_max_99), r
estadd local coin_fe "Yes"
estadd local cond_repeg "No"


eststo: reg d_p d_margin_aprFTX_bps if (l.p_min<p_min_01 | l.p_max>p_max_99) & p_rounded==1, r
estadd local coin_fe "No"
estadd local cond_repeg "Yes"

eststo: reg d_p d_margin_aprFTX_bps i.coin_id if (l.p_min<p_min_01 | l.p_max>p_max_99) & p_rounded==1, r
estadd local coin_fe "Yes"
estadd local cond_repeg "Yes"

eststo: reg d_p d_margin_aprFTX_bps i.coin_id iv_btc funding_apr if (l.p_min<p_min_01 | l.p_max>p_max_99)& p_rounded==1, r
estadd local coin_fe "Yes"
estadd local cond_repeg "Yes"


esttab, drop(*.coin_id _cons) star(* 0.10 ** 0.05 *** 0.01) ar2
esttab using "analysis/output_tables/regression_repeg.tex", replace booktabs label drop(*.coin_id _cons) ///
	prehead(\begin{tabular}{l S S S S S S}   \toprule) ///
	posthead(\midrule) postfoot(\bottomrule\end{tabular}) ///
	style(tex) substitute(\_ _) star  b(2) t(2) r2(2) ///
	nomtitles /// 
	varlabels(iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$" funding_apr "\$\text{Funding Premium}_{t}\$" d_margin_aprFTX_bps "\$\Delta R_{t}\$") /// 
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2 coin_fe cond_repeg, fmt(%9.0fc 2) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"\$N\$"' `"\(R^{2}\)"' `"Coin FE"'  `"Cond. Repeg"'  ))

